package com.flightan.app.parser;

import com.flightan.app.utils.DateParser;
import com.flightan.app.model.Flight;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: Aleks
 * Date: 26.02.12
 * Time: 17:19
 */
public class Kurumoch implements IParser
{
    private static final String DATE_FORMAT = "dd.MM hh:mm";

    private static final String DEPARTURE_URL = "http://airport.samara.ru/phpscripts/tablo/ru/traffic_takeoff_ru.php";
    private static final String DETAIL_DEPARTURE_URL = "http://airport.samara.ru/phpscripts/tablo/ru/takeoffinfo.php";

    private static final String ARRIVAL_URL = "http://airport.samara.ru/phpscripts/tablo/ru/traffic_landing_ru.php";
    private static final String DETAIL_ARRIVAL_URL = "http://airport.samara.ru/phpscripts/tablo/ru/landinginfo.php";

    private List<Flight> getDeparture()
    {
        List<Flight> departureFlights = new ArrayList<Flight>();
        for (List<String> list : getDataString(DEPARTURE_URL))
        {
            if (list.size() > 0)
            {
                Flight flight = new Flight();
                flight.setType("D");
                flight.setCode(list.get(1));
                flight.setTo(list.get(2));
                flight.setCraft(list.get(3));
                flight.setDepartureDate(DateParser.parseTime(list.get(4), DATE_FORMAT));
                flight.setActualDate(DateParser.parseTime(list.get(5), DATE_FORMAT));
                flight.setStatus(list.get(6));
                flight.setFlightID(list.get(7));
                departureFlights.add(flight);
            }
        }
        return departureFlights;
    }

    private List<Flight> getArrival()
    {
        List<Flight> arrivalFlights = new ArrayList<Flight>();
        for (List<String> list : getDataString(ARRIVAL_URL))
        {
            if (list.size() > 0)
            {
                Flight flight = new Flight();
                flight.setType("A");
                flight.setCode(list.get(0));
                flight.setFrom(list.get(1));
                flight.setCraft(list.get(2));
                flight.setArrivalDate(DateParser.parseTime(list.get(3), DATE_FORMAT));
                flight.setActualDate(DateParser.parseTime(list.get(4), DATE_FORMAT));
                flight.setStatus(list.get(5));
                flight.setFlightID(list.get(6));
                arrivalFlights.add(flight);
            }
        }
        return arrivalFlights;
    }

    public Flight getDetailFlightByID(Flight flight)
    {
        if ("A".equals(flight.getType()))
        {
            return getDetailArrivalFlight(flight);
        }
        else if ("D".equals(flight.getType()))
        {
            return getDetailDepartureFlight(flight);
        }
        return null;
    }
    
    private Flight getDetailArrivalFlight(Flight flight)
    {
        try
        {
            Document doc = Jsoup.connect(DETAIL_ARRIVAL_URL + "?" + flight.getFlightID()).get();
            Elements table = doc.getElementsByClass("info");
            Elements td = table.select("td");

            flight.setCode(td.get(2).ownText());
            flight.setAirline(td.get(4).ownText());
            flight.setArrivalDate(DateParser.parseTime(td.get(8).ownText(), DATE_FORMAT));
            flight.setActualDate(DateParser.parseTime(td.get(10).ownText(), DATE_FORMAT));
            flight.setCraft(td.get(12).ownText());
            flight.setFrom(td.get(14).ownText());
            flight.setStatus(td.get(16).ownText());
            return flight;
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private Flight getDetailDepartureFlight(Flight flight)
    {
        try
        {
            Document doc = Jsoup.connect(DETAIL_DEPARTURE_URL + "?" + flight.getFlightID()).get();
            Elements table = doc.getElementsByClass("info");
            Elements td = table.select("td");

            flight.setCode(td.get(2).ownText());
            flight.setAirline(td.get(4).ownText());
            flight.setDepartureDate(DateParser.parseTime(td.get(6).ownText(), DATE_FORMAT));
            flight.setActualDate(DateParser.parseTime(td.get(10).ownText(), DATE_FORMAT));
            flight.setCraft(td.get(12).ownText());
            flight.setRoute(td.get(14).ownText());
            flight.setRegStartDate(DateParser.parseTime(td.get(16).ownText(), DATE_FORMAT));
            flight.setRegEndDate(DateParser.parseTime(td.get(18).ownText(), DATE_FORMAT));
            flight.setRegStatus(td.get(19).ownText());
            flight.setBoardingStartDate(DateParser.parseTime(td.get(21).ownText(), DATE_FORMAT));
            flight.setBoardingEndDate(DateParser.parseTime(td.get(23).ownText(), DATE_FORMAT));
            flight.setBoardingStatus(td.get(24).ownText());
            return flight;
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }


    private List<List<String>> getDataString(String url)
    {
        try
        {
            Document doc = Jsoup.connect(url).get();
            Elements tables = doc.getElementsByClass("info");

            if (tables.size()>0)
            {
                Elements trs = tables.get(0).select("tr");
                List<List<String>> allTrs = new ArrayList<List<String>>(trs.size());

                for (Element tr : trs)
                {
                    Elements tds = tr.select("td");
                    ArrayList<String> objects = new ArrayList<String>(tds.size());
                    String flightId = null;
                    for (Element td : tds)
                    {
                        objects.add(td.ownText());
                        Elements elements = td.select("td[width=80]");
                        if (elements.size() > 0)
                        {
                            String str = elements.outerHtml();
                            int i = str.indexOf("ID");
                            int j = str.indexOf("')\"");
                            flightId = str.substring(i, j);
                        }
                    }
                    if (flightId != null)
                    {
                        objects.add(flightId);
                    }
                    allTrs.add(objects);
                }

                return allTrs;
            }
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public List<Flight> getAllFlights()
    {
        List<Flight> allFlights = getDeparture();
        allFlights.addAll(getArrival());
        return allFlights;
    }

    public List<Flight> getArrivalFlights()
    {
        return getArrival();
    }

    public List<Flight> getDepartureFlights()
    {
        return getDeparture();
    }
}
